{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e2290d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bae3959d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[94 67 92]\n",
      " [72 69 66]\n",
      " [92 65 91]\n",
      " [62 80 90]\n",
      " [83 68 68]\n",
      " [91 75 65]]\n",
      "    0   1   2\n",
      "0  94  67  92\n",
      "1  72  69  66\n",
      "2  92  65  91\n",
      "3  62  80  90\n",
      "4  83  68  68\n",
      "5  91  75  65\n",
      "           0   1   2\n",
      "classA F  94  67  92\n",
      "       M  72  69  66\n",
      "classB F  92  65  91\n",
      "       M  62  80  90\n",
      "classC F  83  68  68\n",
      "       M  91  75  65\n",
      "         Age        \n",
      "         20+ 30+ 40+\n",
      "classA F  94  67  92\n",
      "       M  72  69  66\n",
      "classB F  92  65  91\n",
      "       M  62  80  90\n",
      "classC F  83  68  68\n",
      "       M  91  75  65\n"
     ]
    }
   ],
   "source": [
    "# 创建一个6行3列的随机浮点数, 使用正态分布创建，均值为 85，标准差为3\n",
    "# numbers = np.random.normal(85, 3, (6, 3))\n",
    "# 将浮点数转换成为整数。\n",
    "# data = numbers.astype('int')\n",
    "\n",
    "# 创建一个6行3列的 范围在 60~100 的随机整数\n",
    "data = np.random.randint(60, 100, (6, 3))\n",
    "print(data)\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n",
    "index = [\n",
    "    ('classA', 'F'),\n",
    "    ('classA', 'M'),\n",
    "    ('classB', 'F'),\n",
    "    ('classB', 'M'),\n",
    "    ('classC', 'F'),\n",
    "    ('classC', 'M')\n",
    "]\n",
    "# 把行设置为 复合索引\n",
    "df.index = pd.MultiIndex.from_tuples(index)\n",
    "print(df)\n",
    "\n",
    "# 把列标签 设置为 复合索引\n",
    "columns = [\n",
    "    ('Age', '20+'),\n",
    "    ('Age', '30+'),\n",
    "    ('Age', '40+')\n",
    "]\n",
    "df.columns = pd.MultiIndex.from_tuples(columns)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cf74f95f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         Age        \n",
      "         20+ 30+ 40+\n",
      "classA F  98  64  68\n",
      "       M  95  77  72\n",
      "classB F  61  91  65\n",
      "       M  95  95  94\n",
      "classC F  60  68  61\n",
      "       M  62  83  81\n",
      "------------------------------\n",
      "  Age        \n",
      "  20+ 30+ 40+\n",
      "F  98  64  68\n",
      "M  95  77  72\n",
      "Age  20+    98\n",
      "     30+    64\n",
      "     40+    68\n",
      "Name: (classA, F), dtype: int64\n",
      "         Age        \n",
      "         20+ 30+ 40+\n",
      "classA F  98  64  68\n",
      "       M  95  77  72\n",
      "classC F  60  68  61\n",
      "       M  62  83  81\n",
      "          20+  30+  40+\n",
      "classA F   98   64   68\n",
      "       M   95   77   72\n",
      "classB F   61   91   65\n",
      "       M   95   95   94\n",
      "classC F   60   68   61\n",
      "       M   62   83   81\n",
      "classA  F    98\n",
      "        M    95\n",
      "classB  F    61\n",
      "        M    95\n",
      "classC  F    60\n",
      "        M    62\n",
      "Name: 20+, dtype: int64\n",
      "91\n"
     ]
    }
   ],
   "source": [
    "data = np.random.randint(60, 100, (6, 3))\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "index = [\n",
    "    ('classA', 'F'),\n",
    "    ('classA', 'M'),\n",
    "    ('classB', 'F'),\n",
    "    ('classB', 'M'),\n",
    "    ('classC', 'F'),\n",
    "    ('classC', 'M')\n",
    "]\n",
    "# 把行设置为 复合索引\n",
    "df.index = pd.MultiIndex.from_tuples(index)\n",
    "\n",
    "# 把列标签 设置为 复合索引\n",
    "columns = [\n",
    "    ('Age', '20+'),\n",
    "    ('Age', '30+'),\n",
    "    ('Age', '40+')\n",
    "]\n",
    "df.columns = pd.MultiIndex.from_tuples(columns)\n",
    "\n",
    "print(df)\n",
    "print('-' * 30)\n",
    "# 访问行\n",
    "# 获取访问 A 班的全部信息\n",
    "print(df.loc['classA'])\n",
    "\n",
    "# 获取访问 A 班的全部女生信息\n",
    "print(df.loc['classA', 'F'])\n",
    "\n",
    "# 获取访问 A 班和 C 班的全部信息\n",
    "print(df.loc[['classA', 'classC']])\n",
    "\n",
    "# 访问列\n",
    "# 获取访问'Age' 的 数据\n",
    "print(df['Age'])  # 或者 print(df.Age)\n",
    "\n",
    "# 获取访问'Age' 中 '20+'的 数据\n",
    "print(df.Age['20+'])  # 等同于  print(df['Age', '20+'])\n",
    "\n",
    "# 综合访问\n",
    "# 获取访问 B 班女生 年龄 '30+' 的全部信息\n",
    "print(df.loc['classB', 'F']['Age', '30+'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddf4f83c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
